import sys
import os
import time
import imp
import stackless
from stacklesslib import main
try:
    import stacklessio
except:
    stacklessio = False
from stacklesslib.replacements import thread, threading, popen

def monkeypatch():
    if stacklessio:
        sys.modules['_socket'] = stacklessio._socket
    sys.modules['threading'] = threading
    sys.modules['thread'] = thread
    time.sleep = main.sleep
    os.popen4 = popen.popen4


if (__name__ == '__main__'):
    me = sys.argv.pop(0)
    p = os.path.dirname(me)
    if not p:
        p = '.'
    try:
        sys.path.remove(os.path.abspath(p))
    except ValueError:
        pass
    __name__ = '__slmain__'
    sys.modules['__slmain__'] = sys.modules['__main__']
    del sys.modules['__main__']
    if sys.argv:
        p = os.path.dirname(sys.argv[0])
        if not p:
            p = '.'
        sys.path.insert(0, os.path.abspath(p))

    def Main():
        try:
            try:
                if sys.argv:
                    imp.load_source('__main__', os.path.abspath(sys.argv[0]))
            except Exception, e:
                main.mainloop.exception = sys.exc_info()
                raise 

        finally:
            main.mainloop.running = False



    monkeypatch()
    main.set_scheduling_mode(main.SCHEDULING_ROUNDROBIN)
    stackless.tasklet(Main)()
    main.mainloop.run()

